package com.cuz.daileetcode.bat100;

import com.cuz.daileetcode.utils.ArrayUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class 最小区间 {

    public static void main(String[] args) {
        int[][] by = ArrayUtils.create2By(
                "[[-89,1,69,89,90,98]," +
                "[-43,-36,-24,-14,49,61,66,69]," +
                "[73,94,94,96],[11,13,76,79,90]," +
                "[-40,-20,1,9,12,12,14]," +
                "[-91,-31,0,21,25,26,28,29,29,30]," +
                "[23,88,89]," +
                 "[31,42,42,57]" +
                 ",[-2,6,11,12,12,13,15]" +
                 ",[-3,25,34,36,39]" +
                 ",[-7,3,29,29,31,32,33]" +
               ",[4,11,14,15,15,18,19],[-34,9,12,19,19,19,19,20],[-26,4,47,53,64,64,64,64,64,65],[-51,-25,36,38,50,54],[17,25,38,38,38,38,40],[-30,12,15,19,19,20,22],[-14,-13,-10,68,69,69,72,74,75],[-39,42,70,70,70,71,72,72,73],[-67,-34,6,26,28,28,28,28,29,30,31]]");
        StringBuilder stringBuilder = new StringBuilder();
        for (int[] ints : by) {
            stringBuilder.append(Arrays.toString(ints)+",");
        }
        System.out.println(stringBuilder);
        List<List<Integer>> collect = Arrays.stream(by)
                .map(a -> {
                    ArrayList<Integer> integers = new ArrayList<>();
                    for (int i : a) {
                        integers.add(i);
                    }
                    return integers;
                })
                .collect(Collectors.toList());

        System.out.println(Arrays.toString(solution2(collect)));
    }

    public static int[] solution2(List<List<Integer>> nums) {
        if (nums == null || nums.size() == 0) {
            return new int[]{0, 0};
        }
        TreeSet<Helper> helperTreeSet = new TreeSet<>();
        for (int i = 0; i < nums.size(); i++) {
            List<Integer> integers = nums.get(i);
            Helper helper = new Helper(integers.get(0), integers, 0);
            helperTreeSet.add(helper);
        }
        Helper minInfo = helperTreeSet.first();
        Helper maxInfo = helperTreeSet.last();
        int min = minInfo.value;
        int max = maxInfo.value;
        int range = max - min;
        while (minInfo.fromIndex + 1 < minInfo.from.size()) {
            helperTreeSet.remove(minInfo);
            Helper newH = new Helper(minInfo.from.get(minInfo.fromIndex + 1)
                    , minInfo.from, minInfo.fromIndex + 1);
            helperTreeSet.add(newH);
            minInfo = helperTreeSet.first();
            maxInfo = helperTreeSet.last();
            if (maxInfo.value - minInfo.value < range) {
                min = minInfo.value;
                max = maxInfo.value;
                range = max - min;
            } else if ((maxInfo.value - minInfo.value == range)) {
                if (minInfo.value<min){
                    min=minInfo.value;
                    max=maxInfo.value;
                }
            }
        }
        return new int[]{min, max};
    }


    static class Helper implements Comparable<Helper> {
        //当前值
        int value;
        //来自哪个集合
        List<Integer> from;
        //哪个集合的哪个下表的数
        int fromIndex;

        public Helper(int value, List<Integer> from, int fromIndex) {
            this.value = value;
            this.from = from;
            this.fromIndex = fromIndex;
        }

        @Override
        public int compareTo(Helper o) {
            return value - o.value;
        }
    }
}
